﻿#include "imageviewer.h"
#include <QGraphicsScene>
#include <QWheelEvent>
#include <QMouseEvent>
#include <QScrollBar>
#include <QPainter>
#include <QCursor>

imageViewer::imageViewer(QWidget *parent): QGraphicsView(parent),
    m_scene(new QGraphicsScene(this)),
    m_pixmapItem(nullptr),
    m_panning(false),
    m_zoom(0)
{
    // 绑定场景
    setScene(m_scene);

    // 设置渲染选项（抗锯齿、平滑缩放）
    setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);

    // 缩放和调整锚点设置为“鼠标位置”
    setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
    setResizeAnchor(QGraphicsView::AnchorUnderMouse);
}

/**
 * @brief 加载图片文件
 */
bool imageViewer::loadImage(const QString &filePath)
{
    QPixmap pix(filePath);
    if (pix.isNull())
        return false;  // 图片加载失败

    // 清空旧内容
    m_scene->clear();

    // 添加新图片
    m_pixmapItem = new QGraphicsPixmapItem(pix);
    m_scene->addItem(m_pixmapItem);

    // 设置场景范围与图片大小一致
    setSceneRect(m_pixmapItem->boundingRect());

    // 初始状态下让图片适应窗口大小
    fitInView(m_pixmapItem, Qt::KeepAspectRatio);
    m_zoom = 0;
    return true;
}

/**
 * @brief 鼠标滚轮事件：缩放图片
 */
void imageViewer::wheelEvent(QWheelEvent *event)
{
    if (!m_pixmapItem) return;

    int delta = event->angleDelta().y();  // 滚轮滚动距离（120为一档）
    if (delta == 0) return;

    qreal factor;
    if (delta > 0 && m_zoom < m_zoomMax) {
        // 向上滚动：放大
        factor = m_zoomStep;
        scale(factor, factor);
        m_zoom++;
    } else if (delta < 0 && m_zoom > m_zoomMin) {
        // 向下滚动：缩小
        factor = 1.0 / m_zoomStep;
        scale(factor, factor);
        m_zoom--;
    }
}

/**
 * @brief 鼠标按下事件：开始拖动
 */
void imageViewer::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton && m_pixmapItem) {
        m_panning = true;               // 进入拖动状态
        m_panStart = event->pos();      // 记录起始位置
        setCursor(Qt::ClosedHandCursor);// 改变鼠标形状
        event->accept();
    } else {
        QGraphicsView::mousePressEvent(event);
    }
}

/**
 * @brief 鼠标移动事件：拖动中
 */
void imageViewer::mouseMoveEvent(QMouseEvent *event)
{
    if (m_panning) {
        QPoint delta = event->pos() - m_panStart;  // 本次移动距离
        m_panStart = event->pos();

        // 通过调整滚动条实现平移
        horizontalScrollBar()->setValue(horizontalScrollBar()->value() - delta.x());
        verticalScrollBar()->setValue(verticalScrollBar()->value() - delta.y());

        event->accept();
    } else {
        QGraphicsView::mouseMoveEvent(event);
    }
}

/**
 * @brief 鼠标释放事件：结束拖动
 */
void imageViewer::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton && m_panning) {
        m_panning = false;
        setCursor(Qt::ArrowCursor);
        event->accept();
    } else {
        QGraphicsView::mouseReleaseEvent(event);
    }
}

/**
 * @brief 鼠标双击事件：复位缩放比例
 */
void imageViewer::mouseDoubleClickEvent(QMouseEvent *event)
{
    if (m_pixmapItem) {
        fitInView(m_pixmapItem, Qt::KeepAspectRatio);
        m_zoom = 0;
    }
    QGraphicsView::mouseDoubleClickEvent(event);
}

